package com.iflytek.iot.redis.sharding.core.entity;

import com.iflytek.iot.redis.sharding.core.constants.RedisShardingConstants;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @author ZuRun
 * @date 2020/2/27 17:08:50
 */
@Slf4j
@Accessors(chain = true)
public class SlotConfig {

    /**
     * 建议 0 开始
     */
    private Short slotNo;
    /**
     * 默认是1,建议区间 [-9,10]
     * <p>
     * def ={@link RedisShardingConstants#DEFAULT_VIRTUAL_NODES }
     * -2 = def x 1/3
     * -1 = def x 1/2
     * +0 = def x 1
     * +1 = def x 2
     * +2 = def x 3
     */
    private Short weight = 0;
    private String alias;
    private String nodes;
    private String pwd;
    private String model;
    private String slotName;
    private Object resources;

    public SlotConfig(Short slotNo, Short weight) {
        setSlotNo(slotNo);
        this.weight = weight;
    }

    public SlotConfig(Short slotNo) {
        setSlotNo(slotNo);
    }

    public void setSlotNo(Short slotNo) {
        if (slotNo == null) {
            log.warn("[slot节点为空]");
            throw new IllegalArgumentException("slot节点为空");
        }
        if (slotNo < 0) {
            log.warn("[slot节点错误] slotNo:{}", slotNo);
            throw new IllegalArgumentException("slot节点错误" + slotNo);
        }
        this.slotNo = slotNo;
    }

    public Short getSlotNo() {
        return slotNo;
    }

    public Short getWeight() {
        return weight;
    }

    public SlotConfig setWeight(Short weight) {
        this.weight = weight;
        return this;
    }

    public String getAlias() {
        return alias;
    }

    public SlotConfig setAlias(String alias) {
        this.alias = alias;
        return this;
    }

    public String getNodes() {
        return nodes;
    }

    public SlotConfig setNodes(String nodes) {
        this.nodes = nodes;
        return this;
    }

    public String getPwd() {
        return pwd;
    }

    public SlotConfig setPwd(String pwd) {
        this.pwd = pwd;
        return this;
    }

    public String getModel() {
        return model;
    }

    public SlotConfig setModel(String model) {
        this.model = model;
        return this;
    }

    public String getSlotName() {
        return slotName;
    }

    public SlotConfig setSlotName(String slotName) {
        this.slotName = slotName;
        return this;
    }

    public Object getResources() {
        return resources;
    }

    public SlotConfig setResources(Object resources) {
        this.resources = resources;
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SlotConfig that = (SlotConfig) o;
        return Objects.equals(slotNo, that.slotNo) &&
                Objects.equals(weight, that.weight) &&
                Objects.equals(alias, that.alias) &&
                Objects.equals(nodes, that.nodes) &&
                Objects.equals(pwd, that.pwd) &&
                Objects.equals(model, that.model) &&
                Objects.equals(slotName, that.slotName) &&
                Objects.equals(resources, that.resources);
    }

    @Override
    public int hashCode() {
        return Objects.hash(slotNo, weight, alias, nodes, pwd, model, slotName, resources);
    }
}
